﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Training.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using Microsoft.Reporting.WebForms;
using Training.Library.DomainObject.Administration.Catalog;


namespace Training.Web.Reports 
{

    public partial class rptPelatihanList : BasePage
    {
        private CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                
                //rsViewer.SetExportFormatVisibility(ReportViewerExportFormat.Excel, false);
                //rsViewer.SetExportFormatVisibility(ReportViewerExportFormat.Word, false);


                ddlCabang.Items.Clear();
                List<Cabang> list = CabangDal.GetList();
                Cabang objCabang = CabangDal.GetById(base.CabangID);
                if (objCabang != null)
                {
                    if (!objCabang.NamaCabang.Trim().ToLower().Contains("pusat"))
                    {
                        var query = list.Where(x => x.CabangID == base.CabangID).OrderBy(x => x.NamaCabang);
                        list = query.ToList();

                        ddlCabang.Items.Insert(0, new ListItem("-- pilih cabang --", string.Empty));
                    }
                    else
                    {
                        var query = list.OrderBy(x => x.NamaCabang);
                        list = query.ToList();

                        ddlCabang.Items.Insert(0, new ListItem("-- pilih cabang --", string.Empty));
                    }

                }

                //var query = list.OrderBy(x => x.NamaCabang);
                //list = query.ToList();
                //ddlCabang.Items.Clear();

                list.ForEach(delegate(Cabang obj)
                {
                    ddlCabang.Items.Add(new ListItem(obj.NamaCabang, obj.CabangID.ToString()));
                });


                PembicaraRepo PembicaraDal = new PembicaraRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                List<Pembicara> listPembicara = PembicaraDal.GetList();
                var queryPembicara = listPembicara.OrderBy(x => x.NamaPembicara);
                listPembicara = queryPembicara.ToList();
                ddlPembicara.Items.Clear();

                listPembicara.ForEach(delegate(Pembicara obj)
                {
                    ddlPembicara.Items.Add(new ListItem(obj.NamaPembicara, obj.PembicaraID.ToString()));
                });

                ddlPembicara.Items.Insert(0, new ListItem("-- pilih pembicara --", string.Empty));
            }
        }

        protected void btnProses_Click(object sender, EventArgs e)
        {
            string parameter = string.Empty;
            Cabang objCabang = CabangDal.GetById(base.CabangID);
            if (objCabang != null)
            {
                if (!objCabang.NamaCabang.Trim().ToLower().Contains("pusat"))
                {
                    //param = " WHERE Pelatihan.CabangID=" + base.CabangID;

                    //if (ddlCabang.SelectedIndex == 0)
                    //{
                    //    ClientScript.RegisterClientScriptBlock(typeof(Page), "success", "alert('" + "Pilih cabang sebagai parameter laporan !" + "');", true);
                    //    return;
                    //}
                }

            }

            if (ddlPembicara.SelectedIndex != 0)
                parameter = " WHERE Pelatihan.PembicaraID=" + ddlPembicara.SelectedItem.Value;

            if (ddlCabang.SelectedIndex != 0)
                parameter = parameter == string.Empty ? " WHERE Pelatihan.CabangID=" + ddlCabang.SelectedItem.Value : parameter + " AND Pelatihan.CabangID=" + ddlCabang.SelectedItem.Value;

            if (txtDateStart.Text.Trim().Length == 10)
                parameter = parameter == string.Empty ? " WHERE Convert(varchar(10),CONVERT(date,Pelatihan.TglPelatihan,106),103)='" + txtDateStart.Text +"'" : parameter + " AND Convert(varchar(10),CONVERT(date,Pelatihan.TglPelatihan,106),103)='" + txtDateStart.Text+"'";

            if (txtDateEnd.Text.Trim().Length == 10)
                parameter = parameter == string.Empty ? " WHERE Convert(varchar(10),CONVERT(date,Pelatihan.TglAkhirPelatihan,106),103)='" + txtDateEnd.Text +"'" : parameter + " AND Convert(varchar(10),CONVERT(date,Pelatihan.TglAkhirPelatihan,106),103)='" + txtDateEnd.Text+"'"; 


            PelatihanRepo PelatihanDal = new PelatihanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
            DataTable dt = PelatihanDal.GetDataTableReport(parameter);

            rsViewer.Reset();
            rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/rptPelatihan.rdlc");
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "DataSet1";
            rds.Value = dt;

            rsViewer.LocalReport.DataSources.Clear();
            rsViewer.LocalReport.DataSources.Add(rds);
            rsViewer.LocalReport.Refresh();

        }
    }
}